package com.example.binpacking.service.Impl;

import com.example.binpacking.mapper.InterviewerMapper;
import com.example.binpacking.mapper.SysmasterMapper;
import com.example.binpacking.mapper.TeacherMapper;
import com.example.binpacking.model.dto.IndividualDto;
import com.example.binpacking.service.IndividualService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
@Service
public class IndividualServiceImpl implements IndividualService {
    @Autowired
    private SysmasterMapper sysmasterMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private InterviewerMapper interviewerMapper;
    @Override
    public boolean editUser(IndividualDto dto) {
        // 根据 usertype 确定要更新的表，并执行更新操作
        switch (dto.getUsertype().toLowerCase()) {
            case "s":
                return sysmasterMapper.updateUser(dto.getLoginid(),dto.getName(),dto.getPhone(),dto.getEmail()) > 0;
            case "g":
                return sysmasterMapper.updateUser(dto.getLoginid(),dto.getName(),dto.getPhone(),dto.getEmail()) > 0;
            case "t":
                return teacherMapper.updateUser(dto.getLoginid(),dto.getName(),dto.getPhone(),dto.getEmail()) > 0;
            case "f":
                return interviewerMapper.updateUser(dto.getLoginid(),dto.getName(),dto.getPhone(),dto.getEmail()) > 0;
            default:
                return false;
        }
    }

    @Override
    public List<Map<String, Object>> getUserDetails(IndividualDto dto) {
        List<Map<String, Object>> result;
        switch (dto.getUsertype().toLowerCase()) {
            case "s":
                result = sysmasterMapper.selectUserDetails(dto.getLoginid());
                break;
            case "g":
                result = sysmasterMapper.selectUserDetails(dto.getLoginid());
                break;
            case "t":
                result = teacherMapper.selectUserDetails(dto.getLoginid());
                break;
            case "f":
                result = interviewerMapper.selectUserDetails(dto.getLoginid());
                break;
            default:
                result = Collections.emptyList();
        }

        for (Map<String, Object> map : result) {
            // Ensure all fields are present and convert null to empty strings
            map.putIfAbsent("password", "");
            map.putIfAbsent("loginid", "");
            map.putIfAbsent("orderid", "");
            map.putIfAbsent("phone", "");
            map.putIfAbsent("name", "");
            map.putIfAbsent("createdate", "");
            map.putIfAbsent("isdeleted", "");
            map.putIfAbsent("email", "");
            map.putIfAbsent("parentid", "");
        }

        return result;
    }

}
